package org.erp.spu.controller;

import org.erp.common.domain.Result;
import org.erp.common.page.DataPageResp;
import org.erp.common.page.PageQueryModel;
import org.erp.spu.entity.bo.SpuAttributeBo;
import org.erp.spu.entity.dto.SpuAttributeDto;

import org.erp.spu.entity.vo.SpuAttributeVo;
import org.erp.spu.service.SpuAttributeService;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

import java.util.List;

import static org.erp.common.domain.Result.success;

/**
 * 属性表(SpuAttribute)表控制层
 *
 * @author dingding
 * @since 2025-11-06 09:13:43
 */
@RequestMapping("attribute")
@RestController
public class SpuAttributeController {

    @Resource
    private SpuAttributeService spuAttributeService;
    /**
     * 分页查询所有数据
     *
     * @param spuAttributeBo 筛选条件
     * @return 所有数据
     */
    @GetMapping(value = "/getListByPage")
    public Result<DataPageResp<SpuAttributeVo>> selectAll(SpuAttributeBo spuAttributeBo) {
        SpuAttributeDto spuAttributeDto = new SpuAttributeDto();
        BeanUtils.copyProperties(spuAttributeBo, spuAttributeDto);
        PageQueryModel<SpuAttributeVo> queryModel = spuAttributeService.getListByPage(spuAttributeDto);
        return success(DataPageResp.of(queryModel.getTotal(), queryModel.getDataList()));

    }

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("/{id}")
    public Result<SpuAttributeVo> selectOne(@PathVariable Long id) {
        return success(spuAttributeService.getSpuAttributeVoById(id));
    }
    /**
     * 获取所有数据
     *
     * @param spuAttributeBo 筛选条件
     * @return 所有数据
     */
    @GetMapping("/getList")
    public Result<List<SpuAttributeVo>> getList(SpuAttributeBo spuAttributeBo) {

        return success(spuAttributeService.getList(spuAttributeBo));
    }

    /**
     * 新增数据
     *
     * @param spuAttributeDto
     * @return 新增结果
     */
    @PostMapping("/add")
    public Result<Boolean> insert(@RequestBody SpuAttributeDto spuAttributeDto) {
       boolean result=spuAttributeService.addSpuAttribute(spuAttributeDto);
       return result ? Result.success("新增成功") : Result.error("新增失败");

    }
    /**
     * 修改数据
     *
     * @param spuAttributeDto
     * @return 修改结果
     */
    @PostMapping("/upd")
    public Result<Boolean> update(@RequestBody SpuAttributeDto spuAttributeDto) {
        boolean result = spuAttributeService.updateSpuAttribute(spuAttributeDto);
        return result ? Result.success("修改成功") : Result.error("修改失败");

    }

    /**
     * 删除数据
     *
     * @return 删除结果
     */
    @PostMapping("del")
    public Result<Boolean> delete(@RequestBody SpuAttributeDto spuAttributeDto) {
        boolean result = spuAttributeService.removeById(spuAttributeDto.getId());
        return result ? Result.success("删除成功") : Result.error("删除失败");
    }


}
